/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataAccess;

import java.sql.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 *
 * @author anh
 */
public class DataUtility {

    private static  ResultSet rs;
    private static  CallableStatement cstmt;
    private static  Statement st;
    private static final int _int = 1;
    private static final int _string = 2;
    private static final int _float = 3;

    public DataUtility() {
        
    }

    /**
     *  Retrieves the data follow String query and pass data into a ResultSet
     *
     * @param  str String query
     * @return a Resultset
     * @throws SQLException if String query is incorrect
     */
    public static ResultSet excuteQuery(String str) {
        try {
            Connection con = Connect.getConn();
            st = con.createStatement();
            rs = st.executeQuery(str);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return rs;
    }

    /**
     * Execute a procedure and return a ResultSet
     *
     * @param  proc is name of Procedure
     * @param list is array of parameters
     * @return a Resultset
     * @throws SQLException if String query is incorrect
     */
    public static ResultSet executeProcedure(String proc, HashMap list, boolean flag){
        try {
            initParam(proc, list);
            rs = cstmt.executeQuery();

            return rs;
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally{
            cstmt = null;
        }
        return null;
    }

    /**
     *  Excute a procedure
     *
     * @param  proc String query
     * @param list is array of parameters
     * @throws SQLException if String query is incorrect
     */
    public static void executeProcedure(String proc, HashMap list) {
        try {
            
            initParam(proc, list);
            cstmt.execute();

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally{
            cstmt = null;
        }
    }

    /**
     *  Excute a procedure
     *
     * @param  proc is name of procedure
     * @throws SQLException if String query is incorrect
     */
    public static void executeProcedure(String proc) {
        try {
            Connection con = Connect.getConn();
            cstmt = con.prepareCall("{call " + proc + "}");
            cstmt.execute();
        } catch (SQLException ex) {
           ex.printStackTrace();
        } finally{
            cstmt = null;
        }
    }

    /**
     *  Excute a procedure
     *
     * @param  proc is name of procedure
     * @return a resultset
     * @throws SQLException if String query is incorrect
     */
    public static ResultSet executeProcedure(String proc, boolean flag) {
        try {
            Connection con = Connect.getConn();
            cstmt = con.prepareCall("{call " + proc + "}");
            
            return cstmt.executeQuery();
        } catch (SQLException ex) {
           ex.printStackTrace();
        } finally{
            cstmt = null;
        }
        return null;
    }

    /**
     * Create a object CallableStatement and set parameters from
     * hashmap
     * @param proc name of procedure in database
     * @param list is list parameter passing into procdure
     */
    public static void initParam(String proc, HashMap list){
        try {
            Connection con = Connect.getConn();

            String s = "(";
            for (int i = 0; i < list.size() - 1; i++) {
                s = s + "?,";
            }
            s = s + "?)";
            cstmt = con.prepareCall("{call " + proc + s + "}");
            // Lay mang ten
            Set ListParameter = list.keySet();
            Iterator It = ListParameter.iterator();
            // duyet mang key
            while (It.hasNext()) {
                String paraName = (String) (It.next());
                Object object = list.get(paraName);
                int type = checkType(object);
                setData(type, paraName, object);
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
            cstmt = null;
        }
    }

    /**
     * set data for CallableStatement
     * @param oType is type of data
     * @param paraName is name of parameter in procedure
     * @param object is value was be passed
     * @throws SQLException
     */
    public static  void setData(int oType, String paraName, Object object) throws SQLException {
        if (oType == _int) {
            Integer a = (Integer) object;
            int b = a.intValue();
            cstmt.setInt(paraName, b);
        } else if (oType == _string) {
            String str = (String) object;
            cstmt.setString(paraName, str);
        } else if (oType == _float) {
            Float a = (Float) object;
            float b = a.floatValue();
            cstmt.setFloat(paraName, b);
        }

    }

    /**
     * check type of object
     * @param obj is object was be passed
     * @return a int is type of object
     */
    public static  int checkType(Object obj) {
        if (obj instanceof Integer) {
            return _int;
        } else if (obj instanceof String) {
            return _string;
        } else if (obj instanceof Float) {
            return _float;
        }

        return -1;
    }
}
